/**
  ******************************************************************************
  * @file           : XYXKC_ML307R.h
  * @author         : admin
  * @brief          : None
  * @attention      : None
  * @date           : 2025/1/15
  ******************************************************************************
  */

#ifndef XYXKC_ML307R_H
#define XYXKC_ML307R_H
#include "Arduino.h"
#include "XYXKC_Serial_ml307r.h"
#include <vector>
#include <algorithm>
#define XYXKC_ML307R_BAUD_RATE (115200)

#ifndef  ARDUINO_RUNNING_CORE
#define ARDUINO_RUNNING_CORE 1
#endif
typedef void (*topicMsgFun)(String message);
using namespace std;
enum XYXKC_ML307R_QOS{
    qos0,
    qos1,
    qos2
};
enum XYXKC_ML307R_STATE{
    connectingOrDisconnecting = 1,
    connectionSuccessful,
    connectionDisconnected,
};

class XYXKC_ML307R {
    public:
        XYXKC_ML307R();
        ~XYXKC_ML307R();
        void begin(SoftwareSerial *serial);
        void begin(HardwareSerial *serial);
        void begin(HardwareSerial *serial,int rxPin,int txPin);
        void mqttBegin(const String username,const String password,const String server);
        void mqttBegin(const String username,const String password);
        void mqttStop();
        void mqttSubscribe(String topic,enum XYXKC_ML307R_QOS QOS = qos0);
        void mqttPublish(String topic,String message,enum XYXKC_ML307R_QOS QOS = qos0);
        void ML307R_loop();
        bool isMqttConnectionSuccessful();
        void setMqttMsgCallback(const String topic,const topicMsgFun funName);

    private:
        vector<String> _topics;
        vector<topicMsgFun> _topicMsgFunArray;
        vector<String> _topicFunArray;
        void _delayInit();
        void _getMqttState();
        void _ML307R_Serial_println_look(String msg);
        uint8_t _mqttState = connectionDisconnected;
        XYXKC_Serial_ml307r _ML307R_Serial;
        volatile bool _ML307R_look = false;
};

#endif
